package com.sl.au.controller;

import com.sl.au.entity.*;
import com.sl.au.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping(value = "/ui/direction")
public class DirectionController extends BaseController {

    @Autowired
    DirectionService directionService;

    @Autowired
    AttachmentService attachmentService;

    @Autowired
    OperateLogService operateLogService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = createLayoutView("direction/directionindex");
        Account a = this.getMyAccount();
        String userId=a.getUser().getId();
        modelAndView.addObject("userId", userId);
        String name="directionMng";
        Module module = this.getPageModule(name);
        modelAndView.addObject("module", module);
        return modelAndView;
    }

	@RequestMapping(value = "/getpage", method = RequestMethod.POST)
    public Map<String, Object> getPage(HttpServletRequest request) {
    	Map<String, Object> map = new HashMap<String, Object>();
        Map<String,String> condition = new HashMap<String,String>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        String key=request.getParameter("key");
        Module module = this.getPageModule("directionMng");
        String dataFilter=module.getDataFilter();
        Account a = this.getMyAccount();
        String userId=a.getUser().getId();
        String superior = a.getUser().getSaleArea().getSuperior();
        String areaId=a.getUser().getSaleArea().getId();
        condition.put("key", key);
        condition.put("dataFilter", dataFilter);
        condition.put("userId", userId);
        condition.put("superior", superior);
        condition.put("areaId", areaId);
        List<Direction> directions = directionService.getDirections(start, length,condition,null,null);
        long count =directionService.getDirectionsCount(condition);
        map.put("data", directions);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }

    @RequestMapping(value = "getMobilepage", method = RequestMethod.GET)
    public Map<String, Object> getMobilepage(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> condition = new HashMap<String, Object>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        String key=request.getParameter("key");
        if (key==null){
            key = "";
        }
        Module module = this.getPageModule("directionMng");
        String dataFilter=module.getDataFilter();
        Account a = this.getMyAccount();
        String userId=a.getUser().getId();
        String superior = a.getUser().getSaleArea().getSuperior();
        String areaId=a.getUser().getSaleArea().getId();
        condition.put("key", key);
        condition.put("dataFilter", dataFilter);
        condition.put("userId", userId);
        condition.put("superior", superior);
        condition.put("areaId", areaId);
//        Account a = this.getMyAccount();
//        Module module = this.getPageModule("cir");
//       String dataFilter="本人";
//		/*User user = a.getUser();*/
//       /* User user = new User();
//        user.setId("DFA9DB68-051E-4C65-BD35-758B4D7E7DC4");*/
//        condition.put("key", key);
//        condition.put("dataFilter", dataFilter);
//       condition.put("userId", "DFA9DB68-051E-4C65-BD35-758B4D7E7DC4");
        List<Direction> directions = directionService.getMobilePage(start, length, condition, null, null);
        Long count = directionService.getMobilePageCount(condition);
        map.put("data", directions);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Map<String, Object> saveDirection(HttpServletRequest request, @RequestBody Direction direction) {
        Map<String, Object> map = new HashMap<String, Object>();
        Account a = this.getMyAccount();
        User user = a.getUser();
        direction.setUser(user);
        Direction old=directionService.getRepository().findOne(direction.getId());
        if(old!=null){
            String content  = "编辑去向【" + direction.getActivityName() + "】";
            operateLogService.log(user, "编辑去向", content);
        }else{
            String content  = "新增去向【" + direction.getActivityName() + "】";
            operateLogService.log(user, "新增去向", content);
        }
        directionService.getRepository().save(direction);
        String directionId = direction.getId();
        Date createTime = direction.getCreateTime();
        map.put("code", 0);
        map.put("directionId", directionId);
        map.put("createTime", createTime);
        return map;
    }

    /**
     * 删除去向
     *
     * @return
     */
    @RequestMapping(value = "/delDirection", method = RequestMethod.GET)
    public Map<String, Object> delDirection(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        String ids = request.getParameter("ids");
        String id[] = ids.split(",");
        List<Direction> directions = new ArrayList<Direction>();
        for (int i = 0; i < id.length; i++) {
            String directionId = id[i];
            Direction direction = new Direction();
            direction.setId(directionId);
            directions.add(direction);
           List<Attachment> attachments = new ArrayList<Attachment>();
            attachments = attachmentService.getRepository().findByParentId(directionId);
            attachmentService.getRepository().deleteInBatch(attachments);
        }
        /////插入删除日志
        Account a = this.getMyAccount();
        User user = a.getUser();
        for(Direction d:directions){
            Direction old=directionService.getRepository().findOne(d.getId());
            String content  = "删除去向【" + old.getActivityName() + "】";
            operateLogService.log(user, "删除去向", content);
        }

        directionService.getRepository().deleteInBatch(directions);

        map.put("code", 0);
        return map;
    }
}
